package com.qbw.app.module.vehicle.activity;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.http.okhttp.HttpTask;
import com.qbw.app.R;
import com.qbw.app.XApplication;
import com.qbw.app.databinding.VehicleActivityVoltageBinding;
import com.qbw.app.manager.GeneralManager;
import com.qbw.app.model.Vehicle;
import com.qbw.app.model.Voltage;
import com.qbw.app.module.common.activity.CommonActivity;
import com.qbw.app.module.vehicle.VehicleDataBindingAdapter;
import com.qbw.core.base.BaseActivity;
import com.qbw.core.observable.ObservableBoolean;
import com.qbw.customview.RefreshLoadMoreLayout;
import com.qbw.customview.titlebar.TitleBar;

import static com.qbw.app.request.impl.VehicleRequest.METHOD_VOLTAGE;

/*
 * 电瓶电压
 * @author QBW
 * @date 2019/8/21
 */
public class VehicleVoltageActivity extends CommonActivity<VehicleActivityVoltageBinding> implements TitleBar.Listener, RefreshLoadMoreLayout.CallBack {

    private static final String KEY_VOLTAGE = "voltage";
    private Handler mHandler = new Handler();
    private Voltage mVoltage;

    public static void start(Context context, Voltage voltage) {
        Intent intent = new Intent(context, VehicleVoltageActivity.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        intent.putExtra(KEY_VOLTAGE, voltage);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.vehicle_activity_voltage;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = savedInstanceState == null ? getIntent().getExtras() : savedInstanceState;
        mVoltage = (Voltage) args.getSerializable(KEY_VOLTAGE);
        Vehicle vehicle = mVoltage.getVehicle();
        if (vehicle != null) {
            mBinding.title.setSubTitle(vehicle.getVehicleNick());
            mBinding.title.setSubTitleVisible(true);
        }
        mBinding.title.setListener(this);
        mBinding.setVoltage(mVoltage);
        mBinding.setStandList(mVoltage.getStandardList());
        mBinding.executePendingBindings();

        updateStandard();
        mBinding.rll.init(new RefreshLoadMoreLayout.Config(this).canLoadMore(false));
    }

    private void updateStandard() {
        if (mVoltage.getStandardList() != null) {
            //Voltage.Standard standard = mVoltage.getStandard();
            //mBinding.tvStatus.setText(mVoltage.getStatusTextByStandard(standard));
            mBinding.setStandList(mVoltage.getStandardList());
            VehicleDataBindingAdapter.setVoltageDetailStatusText(mBinding.tvStatus, mVoltage.getState());
            VehicleDataBindingAdapter.setVehicleVoltageRangeText(mBinding.tvVoltageRangeBelongto,
                    mVoltage.getCurrentStandard());

            if (Voltage.EXCEPTION.equals(mVoltage.getState())) {
                mBinding.tvVoltageRangeBelongto.setVisibility(View.GONE);
            } else {
                mBinding.tvVoltageRangeBelongto.setVisibility(View.VISIBLE);
            }
            mBinding.ivBg.setImageResource(mVoltage.getVoltageWaveBgByStandard());
            mBinding.waveProgressbar.setBorderColor(mVoltage.getVoltageWaveBorderColorByStandard());
//            mBinding.waveProgressbar.setFrontWaveColor(mVoltage.getVoltageWaveFrontColorByStandard());
//            mBinding.waveProgressbar.setBehidWaveColor(mVoltage.getVoltageWaveBehindColorByStandard());
            mVoltage.setVoltageWaveColorByStandard(mBinding.waveProgressbar);

        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        int progress = mVoltage.getVoltageProgress();
        if (progress < 0) {
            progress = 0;
        } else if (progress > 100) {
            progress = 100;
        }
        final int p = progress;
        mHandler.postDelayed(() -> mBinding.waveProgressbar.setProgress(65), 100);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mBinding.waveProgressbar.stopWaveAnimation();
    }

    @Override
    public void onLeftAreaClick() {
        finish();
    }

    @Override
    public void onRightAreaClick() {

    }

    @Override
    public void onCenterAreaClick() {

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable(KEY_VOLTAGE, mVoltage);
    }

    @Override
    public void onRefresh() {
        GeneralManager.getInstance()
                      .getRequestManager()
                      .getVehicleRequest()
                      .getVoltage(mVoltage.getVehicle().getVehicleCode(), this)
                      .execute();
    }

    @Override
    public void onLoadMore() {

    }

    @Override
    public void onHttpSuccess(HttpTask httpTask, Object entity) {
        super.onHttpSuccess(httpTask, entity);
        String method = httpTask.getMethod();
        if (METHOD_VOLTAGE.equals(method)) {
            Voltage.Response response = (Voltage.Response) entity;
            Voltage voltage = response.getData();
            voltage.setVehicle(mVoltage.getVehicle());
            mVoltage = voltage;
            mBinding.setVoltage(voltage);
            updateStandard();
            mBinding.rll.stopRefresh();
        }
    }

    @Override
    public void onHttpFailed(HttpTask httpTask, int errorCode, String message) {
        super.onHttpFailed(httpTask, errorCode, message);
        String method = httpTask.getMethod();
        if (METHOD_VOLTAGE.equals(method)) {
            mBinding.rll.stopRefresh();
        }
    }
}
